<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        {include file="public/link"}
    </head>
    
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">

                    <div class="layui-form-item">
                        <label for="username" class="layui-form-label">
                            <span class="x-red">*</span>站名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="title" name="title" required="" lay-verify="title" autocomplete="off" class="layui-input"></div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class="x-red"></span>站点LOGO</label>
                        <div class="layui-input-inline">
                            <div class="layui-upload">
                                <button type="button" class="layui-btn" id="tem_img_up">上传LOGO</button>
                                <div class="layui-upload-list">
                                    <img class="layui-upload-img" style="width: 100%" id="tem_img">
                                    <p id="demoText"></p>
                                    <input type="hidden" name="img" value="" style="width: 500px" id="tem_input_img">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label for="url" class="layui-form-label">
                            <span class="x-red">*</span>站点链接</label>
                        <div class="layui-input-inline">
                            <input type="text" id="url" name="url" required=""  autocomplete="off" class="layui-input">
                        </div>
                            <input value="{$type}" type="hidden" id="type" name="type" required=""  autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-item">
                        <label for="" class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="add" lay-submit="">增加</button>
                    </div>
        </form>
        </div>
        </div>
        <script>
            layui.use('upload', function(){
                var $ = layui.jquery
                    ,upload = layui.upload;

                //普通图片上传
                var uploadInst = upload.render({
                    elem: '#tem_img_up'
                    ,url: "{:url('admin/common/uploadImage')}"
                    ,before: function(obj){
                        //预读本地文件示例，不支持ie8
                        obj.preview(function(index, file, result){
                            $('#tem_img').attr('src', result); //图片链接（base64）
                        });
                    }
                    ,done: function(res){
                        //如果上传失败
                        if(res.code > 0){
                            return layer.msg('上传失败');
                        }
                        //上传成功
                        $('#tem_input_img').attr('value',res.data.src); //图片链接（base64）
                    }
                    ,error: function(){
                        //演示失败状态，并实现重传
                        var demoText = $('#demoText');
                        demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                        demoText.find('.demo-reload').on('click', function(){
                            uploadInst.upload();
                        });
                    }
                });
                //公司logo上传
                var uploadInst = upload.render({
                    elem: '#logo_up'
                    ,url: "{:url('admin/common/uploadImage')}"
                    ,before: function(obj){
                        //预读本地文件示例，不支持ie8
                        obj.preview(function(index, file, result){
                            $('#logo_img').attr('src', result); //图片链接（base64）
                        });
                    }
                    ,done: function(res){
                        //如果上传失败
                        if(res.code > 0){
                            return layer.msg('上传失败');
                        }
                        //上传成功
                        $('#input_logo').attr('value',res.data.src); //图片链接（base64）
                    }
                    ,error: function(){
                        //演示失败状态，并实现重传
                        var demoText = $('#logo');
                        demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                        demoText.find('.logo-reload').on('click', function(){
                            uploadInst.upload();
                        });
                    }
                });
            });

            layui.use(['form', 'layer'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;

                //监听提交
                form.on('submit(add)',
                function(data) {

                    console.log(data.field);
                    var field = data.field;
                    //发异步，把数据提交给php
                    $.ajax({
                        type: "POST",
                        url: "{:url('admin/links/add')}",
                        data:{
                            field:field,
                        },
                        success: function(returnData){
                            console.log('returnData',returnData);
                            if(returnData.code == 200){
                                layer.alert("增加成功", {
                                        icon: 6
                                    },
                                    function() {
                                        // 获得frame索引
                                        var index = parent.layer.getFrameIndex(window.name);
                                        //关闭当前frame
                                        parent.layer.close(index);
                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();

                                    });
                            }else{
                                layer.alert("内部错误", {
                                        icon: 5
                                    },
                                    function() {
                                        // 获得frame索引
                                        var index = parent.layer.getFrameIndex(window.name);
                                        //关闭当前frame
                                        parent.layer.close(index);
                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();
                                    });
                            }
                        }
                    });

                    return false;
                });
            });
        </script>

    </body>

</html>